import axios from 'axios'
import { message } from 'antd';
import Nprogress from 'nprogress'
import 'nprogress/nprogress.css'

// let abc = axios.create({
//     url: "http://localhost:7001"
// })
// let def = axios.create({
//     url: "http://localhost:6001"
// })

// 开发环境  测试环境  预生产环境  生产环境
// let url = ""
// // development
// if(process.env.NODE_ENV === "development") {
//     url = "http://localhost:7001"
// } else if (process.env.NODE_ENV === "production") {
//     url = "http://localhost:abc"
// }

// axios.defaults.baseURL = url

// 白名单 
let whiteList = ["/user/login", "/exam/questions"]

// 请求拦截
axios.interceptors.request.use((config) => {
    Nprogress.start()
    let token = sessionStorage.getItem('token')? sessionStorage.getItem('token'): ''
    config.headers.Authorization = token
    return config
})

// 响应拦截

// 错误处理
axios.interceptors.response.use((data) => {
    Nprogress.done()
    let isMessag = whiteList.some(item => item === data.config.url)
    if (isMessag) message.success(data.data.msg)
    return data
}, (error) => {
    switch(error.response.status) {
        case 500:
            message.error('服务器内部外部错误')
        case 401:
            message.error('没有登陆权限')
        default:
            return 
    }
})

export default axios